﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NetCard.Models;
using System.Web.SessionState;
using Subgurim.Controles;
using NetCard.Dao;
using System.Net.NetworkInformation;

namespace NetCard.Controllers
{

    public class RouterController : Controller
    {
        private RouterDao routerDao = new RouterDao();
        private string url = "http://127.0.0.1:6831/MapIndex.aspx";
        // GET: /Router/

        public ActionResult RouterIndex(Project project)
        {
            return View(project);
        }

        //
        // POST: /Router/Create
        // [HttpPost]
        public void RouterCreate([Bind(Exclude = "id")] Router routerToCreate)
        {
            try
            {
                routerDao.InsertRouter(routerToCreate);
            }
            catch
            {
            }
        }

        //
        // GET: /Router/Edit/5

        public ActionResult RouterEdit()
        {
            GLatLng startPoint = (GLatLng)Session["point"];
            Router routerToEdit = new Router();
            RouterDao routerDao = new RouterDao();
            routerToEdit = routerDao.GetRouterByXY(Decimal.Parse(startPoint.lng.ToString()),
                                                     Decimal.Parse(startPoint.lat.ToString()));
            routerToEdit.x = Decimal.Parse(startPoint.lng.ToString());
            routerToEdit.y = Decimal.Parse(startPoint.lat.ToString());
            return View(routerToEdit);
        }

        //
        // POST: /Router/Edit/5

        [HttpPost]
        public void RouterEdit(Router routerToEdit)
        {
            try
            {
                var originalRouter = routerDao.GetRouter(routerToEdit.id);
                routerDao.UpdateRouter(originalRouter, routerToEdit);
                Response.Redirect(url);
            }
            catch
            {
                Response.Redirect(url);
            }
        }

        [HttpPost]
        public void RouterMove(Router routerToMove)
        {
            try
            {
                var originalItem = routerDao.GetRouter(routerToMove.id);
                routerDao.UpdateRouter(originalItem, routerToMove);
            }
            catch
            {
            }
        }

        //
        // GET: /Router/Delete/5

        public void RouterDelete()
        {
            try
            {

                GLatLng startPoint = (GLatLng)Session["point"];

                RouterDao routerDao = new RouterDao();
                Router routerToDelete = routerDao.GetRouterByXY(Decimal.Parse(startPoint.lng.ToString()),
                                                         Decimal.Parse(startPoint.lat.ToString()));
                var originalRouter = routerDao.GetRouter(routerToDelete.id);
                routerDao.DeleteRouter(originalRouter);
                Response.Redirect(url);
            }
            catch
            {
            }

        }

        //public Router GetRouterByXY(decimal lat, decimal lng)
        //{
        //    Router router = routerDao.GetRouterByXY(lat, lng);
        //    return router;
        //}

        public void RouterTest()
        {
            try
            {

                PingModel pingmdl = new PingModel();
                GLatLng startPoint = (GLatLng)Session["point"];

                RouterDao routerDao = new RouterDao();
                Router routerToTest = routerDao.GetRouterByXY(Decimal.Parse(startPoint.lng.ToString()),
                                                         Decimal.Parse(startPoint.lat.ToString()));
                pingmdl.Ip = routerToTest.ip;

                long projectId = (long)Session["project"];
                ProjectDao projectDao = new ProjectDao();
                Project project = projectDao.GetProject(projectId);
                                  
                pingmdl.Size = (int)project.size;
                pingmdl.TimeOut = (int)project.timeout;
                pingmdl.Ttl = (int)project.ttl;
                this.PingRouter(pingmdl, routerToTest);            
                Response.Redirect(url);
            } catch
            {
                Response.Redirect(url);
            }
        }

        public void AllRoutersTest()
        {
            try
            {
                List<Router> routerList = new List<Router>();
                RouterController routerCtrl = new RouterController();
                RouterDao routerDao = new RouterDao();
                long projectId = (long)Session["project1"];
                ProjectDao projectDao = new ProjectDao();
                Project project = projectDao.GetProject(projectId);

                routerList = routerDao.GetRouters(projectId);

                foreach (Router routerToTest in routerList)
                {
                    PingModel pingmdl = new PingModel();
                    pingmdl.Ip = routerToTest.ip;
                    pingmdl.Size = (int)project.size;
                    pingmdl.TimeOut = (int)project.timeout;
                    pingmdl.Ttl = (int)project.ttl;
                    this.PingRouter(pingmdl, routerToTest);      
                }
                Response.Redirect(url);
            }
            catch
            {
                Response.Redirect(url);
            }
            
        }

        private void PingRouter(PingModel pingmdl, Router routerToTest)
        {
            Pinger ping = new Pinger(routerToTest, pingmdl);
            PingReply reply = ping.Ping();
            if (reply == null)
                return;

            if (reply.Status == IPStatus.Success)
            {
                pingmdl.Ip = reply.Address.ToString();
                pingmdl.Time = reply.RoundtripTime;
                pingmdl.Ttl = reply.Options.Ttl;
                pingmdl.Size = reply.Buffer.Length;

                routerToTest.status = "online";
                RouterController routerCtrl = new RouterController();
                routerCtrl.RouterEdit(routerToTest);
            }
            else
            {
                routerToTest.status = "offline";
                RouterController routerCtrl = new RouterController();
                routerCtrl.RouterEdit(routerToTest);
            }
        }

    }
}
